package com.example.tranport.server;

import com.example.tranport.server.message.ServerMessageService;
import com.example.tranport.server.message.ServerMessageServiceImpl;
import com.example.tranport.server.store.ServerStoreFileService;
import com.example.tranport.server.store.ServerStoreService;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class ServerFactory implements InitializingBean, DisposableBean {

    //服务端端口
    private int port;

    //服务端工具
    private ServerTransPort serverTransPort;

    //消息处理类
    private ServerMessageService serverMessageService;

    //消息存储类
    private ServerStoreService serverStoreService;

    @Override
    public void destroy() throws Exception {
        this.serverMessageService.stop();
        this.serverTransPort.stop();
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        if (this.serverStoreService == null) {
            this.serverStoreService = new ServerStoreFileService();
        }
        if (this.serverMessageService == null) {
            this.serverMessageService = new ServerMessageServiceImpl(this);
            this.serverMessageService.start();
        }
        this.serverTransPort = new ServerTransPortImpl(this);
        this.serverTransPort.start();
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public ServerTransPort getServerTransPort() {
        return serverTransPort;
    }

    public void setServerTransPort(ServerTransPort serverTransPort) {
        this.serverTransPort = serverTransPort;
    }

    public ServerStoreService getServerStoreService() {
        return serverStoreService;
    }

    public void setServerStoreService(ServerStoreService serverStoreService) {
        this.serverStoreService = serverStoreService;
    }

    public ServerMessageService getServerMessageService() {
        return serverMessageService;
    }

    public void setServerMessageService(ServerMessageService serverMessageService) {
        this.serverMessageService = serverMessageService;
    }
}
